import { lazy, type ComponentType } from "react";
import { createBrowserRouter, redirect, type RouteObject } from "react-router";
import LayoutOnlyContent from "../layouts/LayoutOnlyContent";
import LayoutWithHeader from "../layouts/LayoutWithHeader";
import LayoutWithHeaderSider from "@/layouts/LayoutWithHeaderSider";
import { reactCaseList, type caseListType } from "@/siderList";
const pagesCpt = import.meta.glob("@/pages/**/index.tsx", {
  eager: false,
});

//根据siderList生成路由
function generateRoutes(preDir: string, list: caseListType[]): any[] {
  const routeList: RouteObject[] = [];
  list.forEach((item) => {
    if (item.path && item.children) {
      item.children.forEach((i) => {
        if (i.path) {
          routeList.push({
            path: `/${preDir}${item.path}${i.path}`,
            Component: lazy(
              pagesCpt[
                `/src/pages/reactCase/basicCase/useStateCase/index.tsx`
              ] as () => Promise<{ default: ComponentType }>
            ),
          });
        }
      });
    }
  });
  return routeList;
}
console.log(100, generateRoutes("reactCase", reactCaseList));

export default createBrowserRouter([
  {
    path: "/",
    loader: () => redirect("/basicGrammar"), // 移出 LayoutWithHeaderSider
  },
  {
    Component: LayoutOnlyContent,
    children: [
      {
        path: "login",
      },
    ],
  },
  {
    Component: LayoutWithHeader,
    children: [
      {
        path: "home",
      },
    ],
  },
  {
    path: "/",
    Component: LayoutWithHeaderSider,
    children: [
      {
        path: "basicGrammar",
        Component: lazy(
          () => import("@/pages/reactCase/basicCase/components/1-useState")
        ),
      },
      ...generateRoutes("reactCase", reactCaseList),
    ],
  },
]);
